<!-- html5 -->
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/template.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HappyMeal Management System</title>
</head>

<body>
<div id='all'>
  <div id='all-wrap'> 
    
    <!--header begin-->
    <div id='wrap-header'>
      <?php
			include 'header.php';
			//Forbid Non-admin from editing other user
			if($_SESSION['is_admin'] == 0){
				//Auto redirect
				if($_GET['id'] == $_SESSION['id']){
					// Allow editing its own profile
				}else{
					// Forbit editing others profile
					header("Refresh: 2;url=partner.php");
   					die("You do not have the permission to edit a restaurant. Click <a href='partner.php'>here</a> if your browser cannot redirect");
				}
			}
		?>
    </div>
    <!--header end--> 
    
    <!--wrap-body begin-->
    <div id='wrap-body'> 
      <!-- wrap-search begin-->
      <div id='wrap-search'> </div>
      <!-- wrap-search end--> 
      
      <!-- wrap-content begin-->
      <div id='wrap-content'> 
        <!-- nav begin -->
        <div id='nav' style="padding-left:30px;">
          <h4>Current : <a href='main.php'>Main</a>
            <?php include 'nav_png.php'; ?>
            <a href='partner.php'>Restaurant</a>
            <?php include 'nav_png.php'; ?>
            Partner Edit</h4>
        </div>
        <!-- nav END --> 
        <!-- category_add begin-->
        <div id='partner_edit'>
          <?php
					$id = $_GET['id'];
					$sql_partner_id = "SELECT * FROM restaurant WHERE id='$id'";
					$sql_partner_id_result = mysql_query($sql_partner_id);
					if(mysql_num_rows($sql_partner_id_result)==1){
						$sql_partner_id_result_row = mysql_fetch_array($sql_partner_id_result);
						
						$id 		= $sql_partner_id_result_row['id'];
						$is_admin 	= $sql_partner_id_result_row['is_admin'];
						$email 		= $sql_partner_id_result_row['email'];
						$password 	= $sql_partner_id_result_row['password'];
						$name 		= $sql_partner_id_result_row['name'];
						$address 	= $sql_partner_id_result_row['address'];
						$telephone 	= $sql_partner_id_result_row['telephone'];
						$open_hours = $sql_partner_id_result_row['open_hours'];
						$description 		= $sql_partner_id_result_row['description'];
						$profile_picture 	= $sql_partner_id_result_row['profile_picture'];
						$tag 		= $sql_partner_id_result_row['tag'];
						$coupon 	= $sql_partner_id_result_row['coupon'];
						$last_login = $sql_partner_id_result_row['last_login'];
						$is_blocked	= $sql_partner_id_result_row['is_blocked'];
						
					}else{
						//someone typing insame id
						header("Refresh: 2;url=partner.php");
						die("Invalid partner ID entered.");	
					}
                ?>
          <form action='partner.php' method='post' id='partner_edit_form'  enctype="multipart/form-data">
            <h3 style="padding-left:50px;">Please edit the information of the restaurant.</h3>
            <table>
              <tr>
                <td width="150sp">ID:</td>
                <?php
                	echo "<td><input type='text' name='id' value='".$id."' readonly='readonly'></td>";
				?>
              </tr>
              
              <!-- Is administrator? -->
              <tr>
                <td width="150sp">Admin? :</td>
                <td><?php
                									if($_SESSION['is_admin'] == 1){
														//For admin only
													?>
                  <select name='is_admin' value='<?php echo $is_admin; ?>'>
                    <option value=''>----SELECT----</option>
                    <option value='0' <?php if($is_admin=='0'){echo "selected='selected'";}?>>NO</option>
                    <option value='1' <?php if($is_admin=='1'){echo "selected='selected'";}?>>YES</option>
                  </select>
                  <?php
													}else{
														//For non admin
														echo "<input type='text' name='is_admin' value='".$is_admin."' readonly='readonly'><br>";
														echo "You do not have the permission to edit this field";
													}
												   ?></td>
              </tr>
              
              <!-- Email -->
              <tr>
                <td width="150sp">Email:</td>
                <td><input type='email' name='email' value='<?php echo $email; ?>'></td>
              </tr>
              
              <!-- Password -->
              <tr>
                <td width="150sp">Password:</td>
                <td><input type='password' name='password'></td>
              </tr>
              
              <!-- Name -->
              <tr>
                <td width="150sp">Name:</td>
                <td><input type='text' name='name' value='<?php echo $name; ?>'></td>
              </tr>
              
              <!-- Address -->
              <tr>
                <td width="150sp">Address:</td>
                <td><input type='text' name='address' value='<?php echo $address; ?>'></td>
              </tr>
              
              <!-- Telephone -->
              <tr>
                <td width="150sp">Telephone:</td>
                <td><input type='number' name='telephone' value='<?php echo $telephone; ?>'></td>
              </tr>
              
              <!-- Opening Hours-->
              <tr>
                <td width="150sp">Opening Hours:</td>
                <td><textarea rows="4" cols="50" name="open_hours" form="partner_edit_form"><?php echo $open_hours; ?></textarea></td>
              </tr>
              
              <!-- Description -->
              <tr>
                <td width="150sp">Description:</td>
                <td><textarea rows="4" cols="50" name="description" form="partner_edit_form"><?php echo $description; ?></textarea></td>
              </tr>
              
              <!-- Profile Picture -->
              <tr>
                <td width="150sp">Profile Picture:</td>
                <td>Current Picture :<br>
                  <img src='<?php echo $profile_picture; ?>' style="max-width:300px;"/><br>
                  <input type='file' name='profile'>
                  <br>
                  Picture must be less than 1000kb</td>
              </tr>
              
              <!-- Tag -->
              <tr>
                <td width="150sp">Tag:</td>
                <td><table width="100%">
                    <?php
					$tag;
					$tag_array = explode(',', $tag);
					
					$num_of_pair_of_tag = 5;
					for($i = 0; $i < $num_of_pair_of_tag; $i++){
						echo "<tr>";
						echo "<td><input type='text' name='tag[]' value='".$tag_array[(2*$i)]."'></td>";
						echo "<td><input type='text' name='tag[]' value='".$tag_array[(2*$i+1)]."'></td>";
						echo "</tr>";
					}
					
				?>
                  </table></td>
              </tr>
              
              <!-- Coupon -->
              <tr>
                <td width="150sp">Coupon:</td>
                <td>Current Coupon :<br>
                  <img src='<?php echo $coupon; ?>' style="max-width:300px;"/><br>
                  <input type='file' name='coupon'>
                  <br>
                  Picture must be less than 1000kb</td>
              </tr>
              
              <!-- Is blocked? -->
              <tr>
                <td width="150sp">Blocked? :</td>
                <td><?php
					if($_SESSION['is_admin'] == 1){
						//For admin only
					?>
                        <select name='is_blocked' value='<?php echo $is_blocked; ?>'>
                        <option value=''>----SELECT----</option>
                        <option value='0' <?php if($is_blocked=='0'){echo "selected='selected'";}?>>NO</option>
                        <option value='1' <?php if($is_blocked=='1'){echo "selected='selected'";}?>>YES</option>
                        </select>
					<?php
					}else{
						//For non admin
						echo "<input type='text' name='is_admin' value='".$is_blocked."' readonly='readonly'><br>";
						echo "You do not have the permission to edit this field";
					}
				   ?></td>
              </tr>
              
              <!-- Last Login -->
              <tr>
                <td width="150sp">Last Login:</td>
                <td><input type="text" name="last_login" value='<?php echo $last_login;?>' readonly='readonly'></td>
              </tr>
              
              <!-- Submit -->
              <tr>
                <td></td>
                <td align="left"><input type='submit' name='partner_edit' value='Save' onclick="return promptConfirm()">
                  <?php
						if($_SESSION['is_admin'] == 1 && $_SESSION['id']!== $_GET['id']){
							//Only Admin is allowed to delete a restaurant
							echo "<input type='submit' name='partner_delete' value='Delete' onclick='return promptConfirmDelete()'>";
						}
                    ?>
                  <input type='button' name='user_cancel' value='Cancel' onclick='window.location="partner.php";' /></td>
              </tr>
            </table>
          </form>
        </div>
        <!-- category_add end --> 
      </div>
      <!-- wrap-content end--> 
      
    </div>
    <!--wrap-body end--> 
    
    <!--wrap-footer begin-->
    <div id='wrap-footer'>
      <?php
			include 'footer.php';
		?>
    </div>
    <!--wrap-footer end--> 
    
  </div>
  <!-- all-wrap end--> 
</div>
<!-- all end-->
</body>
</html>